public class Homework {
    public int countSegments(String s) {//统计字符串中的单词数
        s = s.trim();
        if(s == null || s.length() == 0){
            return 0;
        }
        String[] strings = s.split(" ");
        int count = 0;
        for(int i = 0; i < strings.length; i++){
            if(strings[i].isEmpty()){
                continue;
            }else{
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String s1 = "         ,,,,,,,   hello world  !    ";
        String s2 = "           ";
        Homework homework = new Homework();
        int num1 = homework.countSegments(s1);
        System.out.println(num1);
        int num2 = homework.countSegments(s2);
        System.out.println(num2);
    }
    public String toLowerCase(String s) {//大写字母转化为小写字母
        if(s == null || s.length() == 0){
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch >= 'A' && ch <= 'Z'){
                ch += 32;
                stringBuilder.append(ch);
            }else{
                stringBuilder.append(ch);
            }
        }
        return stringBuilder.toString();
    }
    public static void main1(String[] args) {
        Homework homework = new Homework();
        String s1 = null;
        String s2 = "here";
        String s3 = "LOVELY";
        s1 = homework.toLowerCase(s1);
        s2 = homework.toLowerCase(s2);
        s3 = homework.toLowerCase(s3);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}
